home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / esc.jar / com / extensibility / rock / ImageEncoder.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-06-30  |  4.0 KB  |  186 lines

  1. package com.extensibility.rock;
  2.  
  3. import java.awt.Image;
  4. import java.awt.image.ColorModel;
  5. import java.awt.image.ImageConsumer;
  6. import java.awt.image.ImageProducer;
  7. import java.io.IOException;
  8. import java.io.OutputStream;
  9. import java.util.Hashtable;
  10.  
  11. public abstract class ImageEncoder implements ImageConsumer {
  12.    protected boolean allowAccumulate;
  13.    protected OutputStream out;
  14.    private ImageProducer producer;
  15.    private int width;
  16.    private int height;
  17.    private int hintflags;
  18.    private boolean started;
  19.    private boolean encoding;
  20.    private IOException iox;
  21.    private static final ColorModel rgbModel = ColorModel.getRGBdefault();
  22.    private Hashtable props;
  23.    private RgbMangler defaultRgbMangler;
  24.    private RgbMangler currentMangler;
  25.    private boolean accumulate;
  26.    private int[] accumulator;
  27.  
  28.    public ImageEncoder(Image var1, OutputStream var2) throws IOException {
  29.       this(var1.getSource(), var2);
  30.    }
  31.  
  32.    public ImageEncoder(ImageProducer var1, OutputStream var2) throws IOException {
  33.       this.allowAccumulate = true;
  34.       this.width = -1;
  35.       this.height = -1;
  36.       this.hintflags = 0;
  37.       this.started = false;
  38.       this.props = null;
  39.       this.defaultRgbMangler = new 1((ImageEncoder)null);
  40.       this.currentMangler = this.defaultRgbMangler;
  41.       this.accumulate = false;
  42.       this.producer = var1;
  43.       this.out = var2;
  44.    }
  45.  
  46.    public void setColorMangler(RgbMangler var1) {
  47.       this.currentMangler = var1;
  48.    }
  49.  
  50.    abstract void encodeStart(int var1, int var2) throws IOException;
  51.  
  52.    abstract void encodePixels(int var1, int var2, int var3, int var4, int[] var5, int var6, int var7) throws IOException;
  53.  
  54.    abstract void encodeDone() throws IOException;
  55.  
  56.    public synchronized void encode() throws IOException {
  57.       this.encoding = true;
  58.       this.iox = null;
  59.       this.producer.startProduction(this);
  60.  
  61.       while(this.encoding) {
  62.          try {
  63.             this.wait();
  64.          } catch (InterruptedException var2) {
  65.          }
  66.       }
  67.  
  68.       if (this.iox != null) {
  69.          throw this.iox;
  70.       }
  71.    }
  72.  
  73.    private void encodePixelsWrapper(int var1, int var2, int var3, int var4, int[] var5, int var6, int var7) throws IOException {
  74.       if (!this.started) {
  75.          this.started = true;
  76.          this.encodeStart(this.width, this.height);
  77.          if ((this.hintflags & 2) == 0 && this.allowAccumulate) {
  78.             this.accumulate = true;
  79.             this.accumulator = new int[this.width * this.height];
  80.          }
  81.       }
  82.  
  83.       if (this.accumulate) {
  84.          for(int var8 = 0; var8 < var4; ++var8) {
  85.             System.arraycopy(var5, var8 * var7 + var6, this.accumulator, (var2 + var8) * this.width + var1, var3);
  86.          }
  87.       } else {
  88.          this.encodePixels(var1, var2, var3, var4, var5, var6, var7);
  89.       }
  90.  
  91.    }
  92.  
  93.    private void encodeFinish() throws IOException {
  94.       if (this.accumulate) {
  95.          this.encodePixels(0, 0, this.width, this.height, this.accumulator, 0, this.width);
  96.          this.accumulator = null;
  97.          this.accumulate = false;
  98.       }
  99.  
  100.    }
  101.  
  102.    private synchronized void stop() {
  103.       this.encoding = false;
  104.       this.notifyAll();
  105.    }
  106.  
  107.    public void setDimensions(int var1, int var2) {
  108.       this.width = var1;
  109.       this.height = var2;
  110.    }
  111.  
  112.    public void setProperties(Hashtable var1) {
  113.       this.props = var1;
  114.    }
  115.  
  116.    public void setColorModel(ColorModel var1) {
  117.    }
  118.  
  119.    public void setHints(int var1) {
  120.       this.hintflags = var1;
  121.    }
  122.  
  123.    public void setPixels(int var1, int var2, int var3, int var4, ColorModel var5, byte[] var6, int var7, int var8) {
  124.       int[] var9 = new int[var3];
  125.  
  126.       for(int var10 = 0; var10 < var4; ++var10) {
  127.          int var11 = var7 + var10 * var8;
  128.  
  129.          for(int var12 = 0; var12 < var3; ++var12) {
  130.             var9[var12] = this.currentMangler.mangleRGBValue(var5.getRGB(var6[var11 + var12] & 255));
  131.          }
  132.  
  133.          try {
  134.             this.encodePixelsWrapper(var1, var2 + var10, var3, 1, var9, 0, var3);
  135.          } catch (IOException var14) {
  136.             this.iox = var14;
  137.             this.stop();
  138.             return;
  139.          }
  140.       }
  141.  
  142.    }
  143.  
  144.    public void setPixels(int var1, int var2, int var3, int var4, ColorModel var5, int[] var6, int var7, int var8) {
  145.       int[] var9 = new int[var3];
  146.  
  147.       for(int var10 = 0; var10 < var4; ++var10) {
  148.          int var11 = var7 + var10 * var8;
  149.          if (var5 == rgbModel) {
  150.             for(int var14 = 0; var14 < var3; ++var14) {
  151.                var9[var14] = this.currentMangler.mangleRGBValue(var6[var11 + var14]);
  152.             }
  153.          } else {
  154.             for(int var12 = 0; var12 < var3; ++var12) {
  155.                var9[var12] = this.currentMangler.mangleRGBValue(var5.getRGB(var6[var11 + var12]));
  156.             }
  157.          }
  158.  
  159.          try {
  160.             this.encodePixelsWrapper(var1, var2 + var10, var3, 1, var9, 0, var3);
  161.          } catch (IOException var13) {
  162.             this.iox = var13;
  163.             this.stop();
  164.             return;
  165.          }
  166.       }
  167.  
  168.    }
  169.  
  170.    public void imageComplete(int var1) {
  171.       this.producer.removeConsumer(this);
  172.       if (var1 == 4) {
  173.          this.iox = new IOException("image aborted");
  174.       } else {
  175.          try {
  176.             this.encodeFinish();
  177.             this.encodeDone();
  178.          } catch (IOException var3) {
  179.             this.iox = var3;
  180.          }
  181.       }
  182.  
  183.       this.stop();
  184.    }
  185. }
  186.